﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DevExpress.Web.ASPxGridView;
using System.Configuration;

public partial class Store_Manager_SM_Product : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
       
        int i = Bestlife.ERP.BusinessRules.productBV_PV.lastproid();
        Productid.Text= i.ToString();
        Session["current"] = "Product";//current page
        if (Session["user"] != null)//check user
        {
            string user = Session["user"].ToString();
            if (user == "viewer")
            {
                Response.Redirect("index.aspx");
            }

        }
        else
        {
            Response.Redirect("index.aspx");
        }

        if (!IsPostBack)
        {
            pointvalue.Text = "0";
         
        }
    }

    //Update
    protected void ASPxGridView1_RowUpdating(object sender, DevExpress.Web.Data.ASPxDataUpdatingEventArgs e)
    {
        string user = Session["username"].ToString();
        try
        {
            Bestlife.ERP.Entities.product pro = new Bestlife.ERP.Entities.product();

            pro.Point_Value = Convert.ToDecimal(e.NewValues["Point_Value"]);
            pro.Cat_ID = Convert.ToString(e.NewValues["Cat_ID"]);
            pro.Pro_ID = Convert.ToString(e.OldValues["Pro_ID"]);
            pro.Pro_Code = Convert.ToString(e.NewValues["Code"]);
            pro.Pro_Des = Convert.ToString(e.NewValues["Description"]);
            pro.Pro_Name = Convert.ToString(e.NewValues["Pro_Name"]);
            pro.Pro_Price = Convert.ToDecimal(e.NewValues["Pro_Price"]);
           
            pro.Pur_Price = Convert.ToDecimal(e.NewValues["Pur_Price"]);
            pro.Ret_Price = Convert.ToDecimal(e.NewValues["Ret_Price"]);
            pro.Brand = Convert.ToString(e.NewValues["brand"]);
            pro.Sub_ID = Convert.ToString(e.NewValues["Sub_Cat"]);
            pro.Bussiness_Value = Convert.ToDecimal(e.NewValues["Business_Value"]);
            pro.Brand = Convert.ToString(e.NewValues["brand"]);
            Bestlife.ERP.BusinessRules.productBV_PV.pvbv(pro, user);
           
        }
        catch
        {
            Label3.Text = "Please Check the data you Entered is in correct format";
        }
    }

    //Insert
    protected void ASPxButton2_Click(object sender, EventArgs e)
    {
        string user = Session["username"].ToString();
        try
        {
            decimal pur = (decimal)(double.Parse(purchaseprice.Text));
            decimal ret = (decimal)(double.Parse(retailprice.Text));
            if (pur <= ret)
            {
                if (purchaseprice.Text == "0.00" || retailprice.Text == "0.00")
                {
                    Label3.Visible = true;
                    Label3.Text = "Prices Cannot be Zero";
                }
                else
                {
                    string[] imageArr = new string[4];

                    Bestlife.ERP.Entities.product pro = new Bestlife.ERP.Entities.product();
                    pro.Cat_Name1 = DropDownList1.Text;

                    pro.Point_Value = (decimal)(double.Parse(pointvalue.Text));
                    pro.Pro_Code = ASPxComboBox1.Text;
                    pro.Pro_Des = productdescription.Text;
                    pro.Pro_ID = Productid.Text;
                    pro.Pro_Name = productname.Text;
                    pro.Pro_Price = (decimal)(double.Parse(productprice.Text));
                    pro.Bussiness_Value = (decimal)(double.Parse(businesspoint.Text));
                    pro.Pur_Price = (decimal)(double.Parse(purchaseprice.Text));
                    pro.Ret_Price = (decimal)(double.Parse(retailprice.Text));
                    pro.Sub_ID = DropDownList2.Text;
                    pro.Brand = brand.Text;
                    pro.Status = DropDownList3.Text;
                    Bestlife.ERP.BusinessRules.productBV_PV.insert(pro, user);

                    //Images Upload
                    // Get the HttpFileCollection
                    if (FileUpload1 != null)
                    {
                        HttpFileCollection hfc = Request.Files;
                        for (int i = 0; i < hfc.Count; i++)
                        {
                            HttpPostedFile hpf = hfc[i];
                            if (hpf.ContentLength > 0)
                            {
                                hpf.SaveAs(MapPath("~/C:/inetpub/vhosts/bestlifecrown.com/httpsdocs/web/img/productimage") + "\\" +
                                  System.IO.Path.GetFileName(hpf.FileName));
                                string image = hpf.FileName;
                                imageArr[i] = image;

                            }
                        }
                        string pid = Productid.Text;
                        Bestlife.ERP.BusinessRules.productBV_PV.insertimage(imageArr, pid);
                    }
                    Label3.Visible = true;
                    Label3.Text = "Product Added Sucessfully!!";


                }
            }
        }
        catch (Exception)
        {

            Label3.Visible = true;
            Label3.Text = "This product is alredy entered to system";
        }
    }

    #region Validation
    protected void ASPxGridView1_RowValidating(object sender, DevExpress.Web.Data.ASPxDataValidationEventArgs e)
    {
        try
        {
            // Displays the error row if there is at least one error.
            if (e.Errors.Count > 0) e.RowError = "Please, fill all fields.";

            if (e.NewValues["Cat_Name"] == null)
            {
                AddError(e.Errors, ASPxGridView1.Columns["Cat_ID"],
                "Please Select Category ID");
            }
            //if (e.NewValues["Sub_Cat_Name"] == null)
            //{
            //    AddError(e.Errors, ASPxGridView1.Columns["Sub_Cat"],
            //    "Please Select Subcategory ID");
            //}
            if (e.NewValues["Brand_Name"] == null)
            {
                AddError(e.Errors, ASPxGridView1.Columns["Brand_Name"],
                "Please Select Category ID");
            }
            if (e.NewValues["Pro_ID"] == null)
            {
                AddError(e.Errors, ASPxGridView1.Columns["Pro_ID"],
                "Please Enter Product ID");
            }
            if (e.NewValues["Code"] == null)
            {
                AddError(e.Errors, ASPxGridView1.Columns["Code"],
                "Please Enter Product Code");
            }
            if (e.NewValues["Description"] != null && e.NewValues["Description"].ToString().Length < 2)
            {
                AddError(e.Errors, ASPxGridView1.Columns["Description"],
                "Description must be at least two characters long.");
            }
            if (e.NewValues["Pro_Name"] == null || e.NewValues["Pro_Name"].ToString().Length < 2)
            {
                AddError(e.Errors, ASPxGridView1.Columns["Pro_Name"],
                "Product Name must be at least two characters long.");
            }
            
            if (e.NewValues["Pro_Price"] == null)
            {
                AddError(e.Errors, ASPxGridView1.Columns["Pro_Price"],
                "Please Enter Product Price");
            }
            if (e.NewValues["Ret_Price"] == null)
            {
                AddError(e.Errors, ASPxGridView1.Columns["Ret_Price"],
                "Please Enter Retail Price ");
            }
            if (e.NewValues["Pur_Price"] == null)
            {
                AddError(e.Errors, ASPxGridView1.Columns["Pur_Price"],
                "Please Enter Purchase Price");
            }



            if (string.IsNullOrEmpty(e.RowError) && e.Errors.Count > 0)
                e.RowError = "Please, correct all errors.";
        }
        catch
        {
            e.RowError = "Please Check The Data You Entered are in Correct Format";
        }
    }

    void AddError(Dictionary<GridViewColumn, string> errors, GridViewColumn column, string errorText)
    {
        try
        {
            if (errors.ContainsKey(column)) return;
            errors[column] = errorText;
        }
        catch
        {
            Response.Redirect("404.aspx");
        }
    }

    protected void ASPxGridView1_StartRowEditing(object sender, DevExpress.Web.Data.ASPxStartRowEditingEventArgs e)
    {
        try
        {
            // Validates the edited row if it isn't a new row,.
            if (!ASPxGridView1.IsNewRowEditing)
                ASPxGridView1.DoRowValidation();
        }
        catch
        {
            Response.Redirect("404.aspx");
        }
    }

    #endregion

    //cancle button
    protected void ASPxButton3_Click(object sender, EventArgs e)
    {
        try
        {
            pointvalue.Text = "";
            
            productdescription.Text = "";
            Productid.Text = "";
            productname.Text = "";
            productprice.Text = "";
       
            purchaseprice.Text = "";
            retailprice.Text = "";
        }
        catch
        {
            Label3.Text = "Cannot Reset.Please try Again.";
        }

    }

    protected void ASPxGridView1_CustomUnboundColumnData(object sender, ASPxGridViewColumnDataEventArgs e)
    {
        try
        {
            if (e.Column.FieldName == "Pro_Price" && e.IsGetData)
            {
                string user = Session["user"].ToString();
                string proid = Productid.Text;
                decimal cost = Convert.ToDecimal(e.GetListSourceFieldValue("Ret_Price"));
                decimal markprice = Convert.ToDecimal(e.GetListSourceFieldValue("Pur_Price"));
                decimal disprice = Bestlife.ERP.BusinessRules.productBV_PV.setdisprice(cost, markprice);
               
               
                e.Value = disprice;

            }

            if (e.Column.FieldName == "Point_Value" && e.IsGetData)
            {
                string user = Session["user"].ToString();
                string proid = Productid.Text;
                decimal cost = Convert.ToDecimal(e.GetListSourceFieldValue("Ret_Price"));
                decimal markprice = Convert.ToDecimal(e.GetListSourceFieldValue("Pur_Price"));
                decimal disprice = Bestlife.ERP.BusinessRules.productBV_PV.setdisprice(cost, markprice);
                decimal pv = Bestlife.ERP.BusinessRules.productBV_PV.setPV(cost, disprice);
                e.Value = pv;
            }
            if (e.Column.FieldName == "Business_Value" && e.IsGetData)
            {
                string user = Session["user"].ToString();
                string proid = Productid.Text;
                decimal markprice = (decimal)(double.Parse(retailprice.Text));
                decimal cost = (decimal)(double.Parse(purchaseprice.Text));
                decimal disprice = Bestlife.ERP.BusinessRules.productBV_PV.setdisprice(cost, markprice);
                decimal pv = Bestlife.ERP.BusinessRules.productBV_PV.setPV(cost, disprice);
                decimal bv = Bestlife.ERP.BusinessRules.productBV_PV.setBV(cost, disprice);
                e.Value = bv;
            }

        }
        catch
        {
            Response.Redirect("404.aspx");
        }
    }


    //PV,BV,Dispro
    protected void retailprice_TextChanged(object sender, EventArgs e)
    {
        try
        {
            if (purchaseprice.Text == "0.00" || retailprice.Text == "0.00")
            {
                Label3.Visible = true;
                Label3.Text = "Prices Cannot be Zero";
            }
            else
            {
                string user = Session["user"].ToString();
                string proid = Productid.Text;
                decimal markprice = (decimal)(double.Parse(retailprice.Text));
                decimal cost = (decimal)(double.Parse(purchaseprice.Text));
                decimal disprice = Bestlife.ERP.BusinessRules.productBV_PV.setdisprice(cost, markprice);
                decimal pv = Bestlife.ERP.BusinessRules.productBV_PV.setPV(cost, disprice);
                decimal bv = Bestlife.ERP.BusinessRules.productBV_PV.setBV(cost, disprice);
                businesspoint.Text = bv.ToString();
                pointvalue.Text = pv.ToString();
                productprice.Text = disprice.ToString();
                Bestlife.ERP.BusinessRules.productBV_PV.insertpvbv(cost, markprice, proid, user);

            }
        }
        catch
        {

        }
    }
    protected void ASPxComboBox1_Init(object sender, EventArgs e)
    {
        ASPxComboBox1.Text = "1";
    }
    
}